using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cframework;
using UnityEngine.TestTools;

public class TestCoroutineWithResult: MonoBehaviour
{
    public IEnumerator Start()
    {
        var b = Test4().WithResult();
        yield return b;

        Debug.LogError("test4 result: " + b.Current);
    }

    public class TestMonoBehaviour: MonoBehaviour
    {

    }

    IEnumerator Test4()
    {   
        Debug.LogError("test StartCoroutine");
        var obj = new GameObject();
        var be = obj.AddComponent<TestMonoBehaviour>();
        yield return be.StartCoroutine(Test5());
        GameObject.DestroyImmediate(obj);
        Debug.LogError("test StartCoroutine");
        yield return Test5();
        Debug.LogError("test WaitForEndOfFrame");
        yield return new WaitForEndOfFrame();
        Debug.LogError("test WaitForFixedUpdate");
        yield return new WaitForFixedUpdate();
        Debug.LogError("test WaitForSeconds");
        yield return new WaitForSeconds(1);
        Debug.LogError("test WaitForSecondsRealtime");
        yield return new WaitForSecondsRealtime(1);
        Debug.LogError("test WaitUntil");
        var startTime = Time.realtimeSinceStartup;
        yield return new WaitUntil(() => Time.realtimeSinceStartup - startTime > 1);
        Debug.LogError("test WaitWhile");
        startTime = Time.realtimeSinceStartup;
        yield return new WaitWhile(() => Time.realtimeSinceStartup - startTime < 1);


        { 
            var b = UnityEngine.Networking.UnityWebRequest.Get("http://www.bdu.com").SendWebRequest();
            Debug.LogError("test " + b.GetType() + " " + b.webRequest.url);
            yield return b;
            Debug.LogError("test " + b.GetType() + " result " + b.webRequest.result);
        }

        {
            var b = UnityEngine.Networking.UnityWebRequest.Get("http://192.168.123.123");
            
            b.timeout = 1;
            var c = b.SendWebRequest();
            Debug.LogError("test " + c.GetType() +  " " + c.webRequest.url);
            yield return c;
            Debug.LogError("test " + c.GetType() + " result " + c.webRequest.result);
        }

        try
        {
            throw new System.Exception("test exception");
        }
        catch(System.Exception e)
        {
            Debug.LogException(e);
        }

        //try
        //{
        //    yield return Test6();
        //}
        //catch (System.Exception e)
        //{
        //    Debug.LogException(e);
        //}

        { 
            var b = Test7().WithResult();
            yield return b;

            yield return b.Current;

            Debug.LogError("test return result: " + b.Current);
        }

        yield return 1;

        Debug.LogError("test");
        yield return 2;
        Debug.LogError("test");

    }

    IEnumerator Test5()
    {
        yield return new WaitForSeconds(1);
    }

    IEnumerator Test6()
    {
        yield return new WaitForSeconds(1);
        throw new System.Exception("test exception test6");
    }

    IEnumerator Test7()
    {
        yield return new WaitForSeconds(1);
        yield return 2;
    }
}
